package com.candy.dao;

import com.candy.pojo.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author donglin
 * @Title:
 * @类名: TestOfDBUtils
 * @date 2020/8/12
 */
public class TestOfDBUtils {
    public static void main(String[] args) {
        test03();
    }

    public static void test03() {
        String strSql="INSERT INTO depts(dept_id,name,descripition) VALUES(?,?,?);";
        Object[] objects={7,"质检部","最后保障"};
//       int i= DBProvider.update(strSql,6,"测试部","找漏洞");
        int i= DBProvider.update(strSql,objects);
       if(i==1){
           System.out.println("新增数据成功！");
       }else{
           System.out.println("新增数据失败！");
       }



    }


    public static void test02(){
        String strSql="select * from tb_student";
       List<Student> list= DBProvider.query(strSql,new BeanListHandler<>(Student.class));
       List<Map<String,Object>> listMap=DBProvider.query(strSql,new MapListHandler());
        List<Object[]> listArray=DBProvider.query(strSql,new ArrayListHandler());
        for (Student eachBean:list
             ) {
            System.out.print("  "+eachBean.getTid());
            System.out.print("  "+eachBean.getName());
            System.out.println();
        }





    }

    public static void test01() {
        String sql="select * from employees";
        QueryRunner runner=new QueryRunner(DBCPDataSource.getDataSource());
        try {
            List<Map<String,Object>> list= runner.query(sql,new MapListHandler());
            for (Map<String,Object> mapEachRow:list
            ) {
                Set<Map.Entry<String,Object>> setEn= mapEachRow.entrySet();
                for (Map.Entry<String,Object> en:setEn
                ) {
                    System.out.print("    "+en.getValue());
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
